/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Codec;

import Message.BufferData;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.apache.mina.filter.codec.CumulativeProtocolDecoder;

import Message.Message;
import java.io.InputStream;
import java.nio.ByteBuffer;

/**
 *
 * @author Nhan Nguyen
 */
public class MessageRequestDecoder extends CumulativeProtocolDecoder
{
//    public static long packet = 0;
//    public static long times = 0;

    public static MessageRequestDecoder getInstance() {
        return InstanceHolder.INSTANCE;
    }

    private static final class InstanceHolder {

        private static final MessageRequestDecoder INSTANCE = new MessageRequestDecoder();
    }

    protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception
    {
        int len = 0;
        int remain = in.remaining();
        boolean isCompleted = in.prefixedDataAvailable(4);

        if ((remain >= 12) && isCompleted) {

            // Remember the initial position.
            int start = in.position();

            int MsgLen = in.getInt() - 8;
            int ActCommand = in.getInt();
            int ServiceType = in.getInt();

//            int packetOrder = in.getInt();
//            int checkSum = in.getInt();

            Message msg = new Message(ActCommand, ServiceType);
            if (MsgLen > 0) {
                byte[] temp = new byte[MsgLen];
                InputStream is = in.asInputStream();
                len = is.read(temp, 0, MsgLen);

                BufferData buffer = msg.getBuffer();
                buffer.ensureAvail(len);
                buffer.writeBytes(temp, 0, len);

                // reset pointer position for read operations
                buffer.setPosition(0);
                temp = null;
            }

//            Object packetCounter = session.getAttribute("packetno");
//            if (packetCounter==null) packetCounter= new Integer(0);

//            VMServer.log.debug(" + Decoded: length=" + MsgLen +
//                    ", Action=" + ActCommand + ", Service=" + ServiceType +
//                    ", order=" + packetOrder + "/"+(Integer)packetCounter +
//                    ", checksum=" + checkSum + "/"+ buffer.getXorCheckSum());

//            session.setAttribute("packetno", (Integer)packetCounter+1);

//            if (System.currentTimeMillis() > times) {
//                times = System.currentTimeMillis() + 30 * 1000;
//                System.out.println("Packet received: "+packet);
//                packet = 0;
//            }

            if (len == MsgLen) {
//                packet++;
                out.write(msg);
                return true;
            }

            // Could not read the buffer fully as specified in MsgLen.
            // Reset the initial position to the one recorded above.
            in.position(start);
        }
        return false;
    }
}
